package com.zabra.zl.ui.allocation_manage;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.orhanobut.logger.Logger;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.AllocationManageBean;
import com.zabra.zl.myview.state_layout.OtherView;
import com.zabra.zl.ui.allocation_manage.contract.AllocationManageContract;
import com.zabra.zl.ui.allocation_manage.presenter.AllocationManagePresenter;
import com.zabra.zl.ui.detailsallocatin.DetailsAllocationActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.SelectorUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AllocationManageActivity extends BaseMvpActivity<AllocationManagePresenter> implements AllocationManageContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_state)
    TextView tvState;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.otherview)
    OtherView otherview;
    @BindView(R.id.refresh)
    PullToRefreshLayout refresh;
    private List<AllocationManageBean.RespDataBean.DataBean> list = new ArrayList<>();
    private List<String> stateList = new ArrayList<>();
    private List<String> typeList = new ArrayList<>();
    private Adp adp;
    private int page = 0;
    private Dialog dialog;
    private String strState = "", strType = "";
    private Bundle bundle = new Bundle();
    private String strJson;

    @Override
    public int getLayoutId() {
        return R.layout.activity_allocation_manage;
    }

    @Override
    public void initView() {
        tvTitle.setText("配资管理");
        otherview.setHolder(mHolder);
        mPresenter = new AllocationManagePresenter();
        mPresenter.attachView(this);
        refresh.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                page = 0;
                mPresenter.getDataFresh(strState, strType, page + "", Config.PAGESIZE);
            }

            @Override
            public void loadMore() {
                page++;
                mPresenter.getDataLoadMore(strState, strType, page + "", Config.PAGESIZE);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        page = 0;
        mPresenter.getData(strState, strType, page + "", Config.PAGESIZE);
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
        refresh.finishRefresh();
        refresh.finishLoadMore();
    }

    @Override
    public void onSuccess(AllocationManageBean bean) {
        initData(bean);
    }

    @Override
    public void onSuccessFresh(AllocationManageBean bean) {
        refresh.finishRefresh();
        initData(bean);
    }

    @Override
    public void onSuccessLoadMore(AllocationManageBean bean) {
        refresh.finishLoadMore();
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            list.addAll(bean.getRespData().getData());
            //延迟更新数据，避免卡顿
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    adp.notifyItemRangeChanged(0, bean.getRespData().getData().size());
                }
            }, 500);
            if (bean.getRespData().getData().size() == 0) {
                ToastUtil.showToast(this, "没有更多数据");
            }
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }


    @OnClick({R.id.back, R.id.tv_state, R.id.tv_all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_state:
                chooseState();
                break;
            case R.id.tv_all:
                chooseType();
                break;
        }
    }

    private void chooseState() {
        stateList.clear();
        stateList.add("操盘中");
        stateList.add("申请结束");
        stateList.add("终止");
        OptionsPickerView pvNoLinkOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                tvState.setText(stateList.get(options1));
                strState = (options1 + 1) + "";
                page = 0;
                mPresenter.getData(strState, strType, page + "", Config.PAGESIZE);
            }
        }).setCancelColor(Color.parseColor("#999999"))
                .setSubmitColor(Color.parseColor("#ff3025"))
                .setTitleBgColor(Color.parseColor("#ffffff"))
                .setContentTextSize(14)
                .setSubCalSize(14)
                .setContentTextSize(18)
                .setCancelText("全部")
                .setOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        tvState.setText("全部");
                        strState = "";
                        page = 0;
                        mPresenter.getData(strState, strType, page + "", Config.PAGESIZE);
                    }
                })
                .build();
        pvNoLinkOptions.setNPicker(stateList, null, null);
        pvNoLinkOptions.show();
    }

    private void chooseType() {
        typeList.clear();
        typeList.add("按日");
        typeList.add("按月");
        typeList.add("微策略");
        OptionsPickerView pvNoLinkOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                tvAll.setText(typeList.get(options1));
                strType = (options1 + 1) + "";
                page = 0;
                mPresenter.getData(strState, strType, page + "", Config.PAGESIZE);
            }
        }).setCancelColor(Color.parseColor("#999999"))
                .setSubmitColor(Color.parseColor("#ff3025"))
                .setTitleBgColor(Color.parseColor("#ffffff"))
                .setContentTextSize(14)
                .setSubCalSize(14)
                .setContentTextSize(18)
                .setCancelText("全部")
                .setOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        tvAll.setText("全部");
                        strType = "";
                        page = 0;
                        mPresenter.getData(strState, strType, page + "", Config.PAGESIZE);
                    }
                })
                .build();
        pvNoLinkOptions.setNPicker(typeList, null, null);
        pvNoLinkOptions.show();
    }

    private void initData(AllocationManageBean bean) {
        list.clear();
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            list.addAll(bean.getRespData().getData());
            if (list.size() == 0) {
                otherview.showEmptyView();
                refresh.setCanLoadMore(false);
            } else {
                refresh.setCanLoadMore(true);
                otherview.showContentView();
            }
            recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
            recycler.setNestedScrollingEnabled(false);
            adp = new Adp(R.layout.item_allocation_manage, list);
            recycler.setAdapter(adp);
            adp.openLoadAnimation();
            adp.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    AllocationManageBean.RespDataBean.DataBean dataBean = list.get(position);
                    Gson gson = new Gson();
                    strJson = gson.toJson(dataBean);
                    bundle.putString("bean", strJson);
                    toClass(view.getContext(), DetailsAllocationActivity.class, bundle);
                }
            });
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

}
